import Restful from './api/http.restful.api';
import setting from '/@/settings/componentSetting.ts';
import dictionary from './render/src/dictionary';

import Render from './render/src/render';
import { getQueryParams, transRegion } from './render/src/function';

import Button from './render/classes/button';
import Form from './render/classes/form';
import Search from './render/classes/search';

class Page {
  // 请求后端的name值，由后端解析
  public name = '';
  // 字典
  private _dictionaries: object = [];
  // 搜索表单
  private _search = {
    items: [],
    before: null,
  };
  // 编辑、新增表单
  private _form = {
    items: [],
    value: {},
  };

  private _table = {
    header: [],
    toolbar: ['plus', 'remove', 'download'],
    trash: ['recovery', 'clean'],
    action: ['edit', 'remove'],
    dropAction: [],
    api: null,
    after: null,
    showIndexColumn: true,
    rowSelection: true,
    trans: [],
    region: false,
  };

  private useButton = false;

  constructor(name = '') {
    this.dictionay = {};
    this.name = name;
  }
  // 设置字典，这里引入了常规的字段信息，如果设置了则进行覆盖
  set dictionay(dictionaries: object) {
    // 防止污染
    const labels = JSON.parse(JSON.stringify(dictionary));
    for (const i in dictionaries) {
      labels[i] = dictionaries[i].text;
    }
    this._dictionaries = labels;
  }
  get dictionay() {
    return this._dictionaries;
  }

  set search(param: Array) {
    this._search.items = param;
  }
  get search() {
    return this._search;
  }

  set form(param: Array) {
    this._form.items = param;
  }
  get form() {
    return this._form;
  }

  set table(param: Array) {
    this._table.header = param;
  }
  get table() {
    return this._table;
  }

  public onlyShow() {
    this._table.showIndexColumn = false;
    this._table.rowSelection = false;
    this._table.toolbar = [];
    this._table.trash = [];
  }

  public useForm() {
    return new Render(this.dictionay).form(this._form.items, this._form.value);
  }

  public useTable() {
    const render = new Render(this.dictionay);
    if (!this.useButton) {
      this._table.toolbar = render.toolbar(this._table.toolbar);
      this._table.trash = render.toolbar(this._table.trash);
      this._table.action = render.action(this._table.action);
      this._table.dropAction = render.dropActions(this._table.dropAction);
      this.useButton = true;
    }
    const api = (params) => {
      return Restful.list(this.name, params);
    };
    return {
      columns: render.header(this._table.header),
      // api请求配置
      api: api,
      fetchSetting: setting.table.fetchSetting,
      formConfig: {
        labelAlign: 'right',
        labelCol: {
          span: 6,
        },
        showAdvancedButton: true,
        actionColOptions: {
          span: 24,
        },
        schemas: render.search(this._search.items),
      },
      // 请求之前参数处理
      beforeFetch: (params) => {
        if (this.withTrash) {
          params._trash = 1;
        } else {
          params._trash = 0;
        }
        const before = this._search.before;
        if (typeof before === 'function') {
          before(params);
        }
        const query = getQueryParams();
        // 处理url中的参数
        for (const i in params) {
          if (!params[i]) {
            params[i] = query[i] === undefined ? '' : query[i];
          }
        }
        return params;
      },
      // 请求之后的处理
      afterFetch: (resp) => {
        if (this.table.region) {
          for (const data of resp) {
            const regions = [];
            if (parseInt(data.province_id) > 0) {
              regions.push(data.province_id);
            }
            if (parseInt(data.city_id) > 0) {
              regions.push(data.city_id);
            }
            if (parseInt(data.area_id) > 0) {
              regions.push(data.area_id);
            }
            data.region_text = transRegion(regions);
          }
        }
        if (Object.keys(this.trans).length > 0) {
          for (const item of resp) {
            for (const field in this.trans) {
              // 列表中对应字段的值
              const val = item[field];
              // 选项信息
              const options = this.trans[field];
              // 去选项中的值
              const val_text = options[val] ? options[val] : '';
              item[field + '_text'] = val_text;
            }
          }
        }
        const after = this._table.after;
        if (typeof after === 'function') {
          after(resp);
        }
      },
    };
  }

  private trans: any = {};
  /**
   * @param field 要转化的字段信息
   * @param options 对应的信息
   *
   * field=status
   * options={1:待支付,2:已支付}
   *
   * 当表格中的status字段值为1的时候，将产生一个status_text字段，且值为待支付
   *
   */
  public setTrans(field: string, options: object) {
    if (field) {
      this.trans[field] = options;
    }
  }
  public setRegionTrans() {
    this.trans['region'] = [];
  }

  /**
   * 是否加载回收站的数据
   */
  private _withTrash = false;
  set withTrash(trash = false) {
    this._withTrash = trash;
  }
  get withTrash() {
    return this._withTrash;
  }
}

export { Page, Form, Button, Search, Restful };
